﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MrPoetic.MatLab
{
    /// <summary>
    /// 丁诚昊的数字游戏工具箱 可自动生成随机字母数字的组合
    /// </summary>
    public sealed partial class DataMatrix
    {
        public enum AIPuzzleType
        {
            /// <summary>
            /// 纯数字
            /// </summary>
            Alpha = -1,
            /// <summary>
            /// 纯字母
            /// </summary>
            Lett = -2,
            /// <summary>
            /// 字母数字混合
            /// </summary>
            All = Alpha * Lett,
        }
        /// <summary>
        /// 获取 a~z - 包含大小写
        /// </summary>
        /// <returns> 获取 a~z - 长度 27*2=54个 </returns>
        static public char[] GetLett()
        {
            var table = Enumerable.Range(0, 255)
                        .Where(o => (char)o >= 'A' && (char)o <= 'Z'
                        || ((char)o >= 'a' && (char)o <= 'z'))
                        .Select(o => (char)o);
            return table.ToArray();
        }
        static internal char[] GetAlpha()
        {
            return HexTable.Take(10).ToArray<char>();
        }
        /// <summary>
        /// 生成指定位数的随机字符串 - 丁诚昊:这实际上是一个俄罗斯轮盘算法
        /// </summary>
        /// <param name="bit">需要生成的位数</param>
        /// <param name="GenType">分别制定生成 1.是否纯数字 2.数字+字母 3.纯字母</param>
        /// <returns></returns>
        static public string GenAnyLetters(int bit, AIPuzzleType GenType = AIPuzzleType.All)
        {
            StringBuilder buffer = new StringBuilder(bit);

            LoopUntilOK:
            var rsList = new List<char>();
            switch (GenType)
            {
                // 合并参数集合 为待筛选结果集
                case AIPuzzleType.All:
                    rsList = GetLett().Union(GetAlpha()).ToList();
                    break;
                case AIPuzzleType.Alpha:
                    rsList = GetAlpha().ToList();
                    break;
                case AIPuzzleType.Lett:
                    rsList = GetLett().ToList();
                    break;
            }
            // 俄罗斯轮盘 1.通用版 2.数字版 + 3.字符集版
            var count = rsList.Count();
            while (bit-- > 0)
            {
                if (rsList.Count == 0)
                    goto LoopUntilOK;

                var index = Seed.Next(0, rsList.Count);
                char charactor = rsList.ElementAt(index);
                buffer.Append(charactor);
                rsList.RemoveAt(index);
            }
            return buffer.ToString();
        }
    }
}
